package com.example.Servlet;

import com.example.Service.UserService;
import com.example.pojo.User;
import com.google.gson.Gson;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "getAllUser", value = "/getAllUser")
public class getAllUser extends HttpServlet {
    private String message;

    public void init() {
        message = "Hello World!";
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        response.setContentType("text/html");
        String page=request.getParameter("page");
        int currentPage=0;
        if(page==null){
            currentPage=1;
        }else{
            currentPage=Integer.parseInt(page);
        }
        int pageSize=Integer.parseInt(request.getParameter("pageSize"));
        //2.调用业务逻辑，完成查询功能
        UserService userService=new UserService();
        List<User> list =userService.getAllUser(currentPage,pageSize);
        int total=userService.getTotal();
        //3.响应给前端，Json数据
        Map<String,Object> map=new HashMap<>();
        map.put("code",200);
        map.put("msg","查询成功");
        map.put("total",total);
        map.put("data",list);

        Gson gson=new Gson();
        String json= gson.toJson(map);
        response.getWriter().append(json);

    }

    public void destroy() {
    }
}